package com.example.administrator.im.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.resources.TextAppearance;
import android.support.design.resources.TextAppearanceConfig;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.im.R;
import com.example.administrator.im.interfacz.ShareTitleable;

/**
 * create:2018/11/7 0:14
 * author: 姚鸿杰
 **/
public class ShareTitle extends RelativeLayout {

    private int background;
    private int left_icon;
    private String title;
    private int right_icon;

    private RelativeLayout rootLayout;
    private ImageView iv_left;
    private TextView tv_title;
    private ImageView iv_right;

    public ShareTitle(Context context) {
        super(context);
    }

    public ShareTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        initView(context);
    }

    public ShareTitle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        initView(context);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ShareTitle);

        background = ta.getColor(R.styleable.ShareTitle_bg, Color.argb(0, 25, 164, 185));
//        background = ta.getColor(R.styleable.ShareTitle_bg, -1);
        left_icon = ta.getResourceId(R.styleable.ShareTitle_left_icon, -1); //设置左边的图标
        title = ta.getString(R.styleable.ShareTitle_title); //标题内容
        right_icon = ta.getResourceId(R.styleable.ShareTitle_right_icon, -1); //右边图标

        ta.recycle();
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.share_title, this, true);

        rootLayout = (RelativeLayout) findViewById(R.id.root_title);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_right = (ImageView) findViewById(R.id.iv_right);

        rootLayout.setBackgroundColor(background);
        if (left_icon == -1) {
            iv_left.setVisibility(View.GONE);
        } else {
            iv_left.setImageResource(left_icon);
        }

        iv_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                shareTitleListener.left_click(v);
            }
        });

        if (right_icon == -1) {
            iv_right.setVisibility(View.GONE);
        } else {
            iv_right.setImageResource(right_icon);
        }

        iv_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                shareTitleListener.right_click(v);
            }
        });

        setTitle(title, Typeface.BOLD);
    }

    public void setTitle(String title, int typeface) {
        if (!TextUtils.isEmpty(title))
            tv_title.setText(title);
        else
            tv_title.setVisibility(View.GONE);
        tv_title.setTypeface(Typeface.defaultFromStyle(typeface));
    }

    private ShareTitleable shareTitleListener;

    public void setIconOnClickListener(ShareTitleable shareTitleListener) {
        this.shareTitleListener = shareTitleListener;
    }

    public void setLeftListener(OnClickListener onClickListener) {
        iv_left.setOnClickListener(onClickListener);
    }

    public void setRightListener(OnClickListener onClickListener) {
        iv_right.setOnClickListener(onClickListener);
    }
}
